
//
//  HomePageHeaderView.swift
//  民生生活
//
//  Created by apple on 2017/8/7.
//  Copyright © 2017年 youjindi.app.com. All rights reserved.
//

import UIKit
import ReactiveCocoa

class HomePageHeaderView: UIView,SDCycleScrollViewDelegate {

    var dataArray:[HomePageModel] = []
    var imageArray:[String] = []
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        layout()
        getData()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func getData() -> Void {
        viewModel.getHeadImageArray { (dataArray) in
            self.dataArray = dataArray as! [HomePageModel]
            for object in self.dataArray{
                self.imageArray.append(ImageUrl + object.n_pic!)
            }
            self.header.imageURLStringsGroup = self.imageArray
        }
    }
    
    func layout() -> Void{
        backgroundColor = MainBackGroundColor
        
        let titleA:[String] = ["火车票","违章代缴","话费充值","流量充值","快递下单","域名查询","快递查询","水电煤充值"]
        let commandA = [viewModel.checkForTrainAction,
                        viewModel.checkForBreakRule,
                        viewModel.checkForPayForPhone,
                        viewModel.checkForPayForPhoneData,
                        viewModel.checkForPayForPhoneData,
                        viewModel.checkForPayForPhoneData,
                        viewModel.checkForPayForPhoneData,
                        viewModel.checkForPayForPhoneData,
                        viewModel.checkForPayForPhoneData,
                        viewModel.checkForPayForPhoneData,
                        viewModel.checkForPayForPhoneData]
        let btnW = ScreenWidth / 4
        let cv = UIView(frame: CGRect(x: 0, y: 170, width: ScreenWidth, height: 160))
        
        cv.backgroundColor = UIColor.white
        addSubview(cv)
        
        for index in 0..<titleA.count{
            var btnPoint = CGPoint(x: CGFloat(index) * btnW, y: 0)
            if (btnPoint.x + 20 > ScreenWidth){
                btnPoint = CGPoint(x: CGFloat(index - 4) * btnW, y: 80)
            }
            let btnFrame = CGRect(x: btnPoint.x, y: btnPoint.y, width: btnW, height: 80)
            let btn = AponeBtn(titleA[index],titleA[index],btnFrame,commandA[index])
            cv.addSubview(btn)
            
        }
        
    }
    
    func cycleScrollView(_ cycleScrollView: SDCycleScrollView!, didSelectItemAt index: Int) {
        
        let model = dataArray[index]
        
        
    }
    
    lazy var header: SDCycleScrollView = {
        let header = SDCycleScrollView()
        header.frame = CGRect(x: 0, y: 0, width: ScreenWidth, height: 160)
        header.pageControlAliment = .init(rawValue: 0)
        header.placeholderImage = PlaceHolderImage
        header.delegate = self
        self.addSubview(header)
        return header
    }()
    
    lazy var viewModel: HomePageViewModel = {
        let viewModel = HomePageViewModel()
        return viewModel
    }()
    
}
